
/*********************
 **  CreateTime: 2006-7-26 
 **********************/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using EntLibEC.Cmn;
using EntLibEC.Info.PIM;


namespace EntLibEC.Dac.PIM
{
    /// <summary>
    /// Contains the the data access methods for OutputTemplateDA.
    /// </summary>
    public abstract class OutputTemplateDA
    {
        #region Sql Script

        private const string SQL_GET_OUTPUTTEMPLATE
            = @"SELECT a.SysNo,a.TargetType,a.TargetSysNo,a.TemplateType,a.Template,a.LastEditUserSysNo,a.LastEditTime,b.UserName as LastEditUsername 
				FROM PIM_OutputTemplate a (NOLOCK)
                LEFT JOIN Ent_Sys_User b (NOLOCK) ON a.LastEditUserSysNo = b.SysNo
				WHERE TargetSysNo = @TargetSysNo AND TargetType  =@TargetType";

        private const string SQL_INSERT_OUTPUTTEMPLATE = "INSERT INTO PIM_OutputTemplate(TargetType,TargetSysNo,TemplateType,Template,LastEditUserSysNo,LastEditTime) VALUES (@TargetType,@TargetSysNo,@TemplateType,@Template,@LastEditUserSysNo,getdate()) SELECT @@IDENTITY";
        private const string SQL_UPDATE_OUTPUTTEMPLATE = "UPDATE PIM_OutputTemplate SET TargetType=@TargetType,TargetSysNo=@TargetSysNo,TemplateType=@TemplateType,Template=@Template,LastEditUserSysNo=@LastEditUserSysNo,LastEditTime=getdate() WHERE SysNo=@SysNo";
        private const string SQL_DELETE_OUTPUTTEMPLATE = "DELETE FROM PIM_OutputTemplate WHERE SysNo = @SysNo AND TargetType=@TargetType";
        #endregion

        protected static OutputTemplateInfo Map(DataRow row)
        {
            OutputTemplateInfo info = new OutputTemplateInfo();

            info.SysNo = Convert.ToInt32(row["SysNo"]);
            info.TargetType =(OutputTemplateTargetType)Convert.ToInt32(row["TargetType"]);
            info.TargetSysNo = Convert.ToInt32(row["TargetSysNo"]);
            info.TemplateType = (OutputTemplateType)Convert.ToInt32(row["TemplateType"]);
            info.PropertiesIdList = CommonFunctions.GetIntListFromString(row["Template"].ToString(), ',');
            info.LastEditUserSysNo = Convert.ToInt32(row["LastEditUserSysNo"]);
            info.LastEditTime = Convert.ToDateTime(row["LastEditTime"]);

            info.LastEditUsername = row["LastEditUsername"].ToString();

            return info;
        }

        public static List<OutputTemplateInfo> GetOutputTemplates(OutputTemplateTargetType TargetType, int targetSysNo)
        {
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter("@TargetSysNo", SqlDbType.Int),
                new SqlParameter("@TargetType", SqlDbType.Int)
        };
            parms[0].Value = targetSysNo;
            parms[1].Value = (int)TargetType;

            DataTable dt = DataAccessFacade.GetDataTable(SQL_GET_OUTPUTTEMPLATE, parms);
            if (dt == null)
                return null;
            List<OutputTemplateInfo> list = new List<OutputTemplateInfo>();
            foreach (DataRow row in dt.Rows)
                list.Add(Map(row));
            return list;
        }

        public static int InsertOutputTemplate(OutputTemplateInfo info)
        {
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter("@SysNo", SqlDbType.Int),
                new SqlParameter("@TargetType", SqlDbType.Int),
                new SqlParameter("@TargetSysNo", SqlDbType.Int),
                new SqlParameter("@TemplateType", SqlDbType.Int),
                new SqlParameter("@Template", SqlDbType.VarChar,100),
                new SqlParameter("@LastEditUserSysNo", SqlDbType.Int)
            };

            parms[0].Value = info.SysNo;
            parms[1].Value = (int)info.TargetType;
            parms[2].Value = info.TargetSysNo;
            parms[3].Value = (int)info.TemplateType;
            parms[4].Value = CommonFunctions.GetStringFromIntList(info.PropertiesIdList);
            parms[5].Value = info.LastEditUserSysNo;

            object obj = DataAccessFacade.ExecScalarSqlCommand(SQL_INSERT_OUTPUTTEMPLATE, parms);
            return Convert.ToInt32(obj);
        }

        public static void UpdateOutputTemplate(OutputTemplateInfo info)
        {
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter("@SysNo", SqlDbType.Int),
                new SqlParameter("@TargetType", SqlDbType.Int),
                new SqlParameter("@TargetSysNo", SqlDbType.Int),
                new SqlParameter("@TemplateType", SqlDbType.Int),
                new SqlParameter("@Template", SqlDbType.VarChar, 100),
                new SqlParameter("@LastEditUserSysNo", SqlDbType.Int)
             };

            parms[0].Value = info.SysNo;
            parms[1].Value = (int)info.TargetType;
            parms[2].Value = info.TargetSysNo;
            parms[3].Value = (int)info.TemplateType;
            parms[4].Value = CommonFunctions.GetStringFromIntList(info.PropertiesIdList);
            parms[5].Value = info.LastEditUserSysNo;

            DataAccessFacade.ExecSqlCommand(SQL_UPDATE_OUTPUTTEMPLATE, parms);
        }

        public static void DeleteOutputTemplate(int SysNo)
        {
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter("@SysNo", SqlDbType.Int)
            };
            parms[0].Value = SysNo;

            DataAccessFacade.ExecSqlCommand(SQL_DELETE_OUTPUTTEMPLATE, parms);
        }
    }

}
